package io.wdb.modules.generator.generator.service.impl.book;

import java.util.Date;
import java.util.List;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import com.github.pagehelper.PageHelper;

import io.wdb.common.utils.PageHelperUtil;
import io.wdb.common.utils.Query;
import io.wdb.modules.generator.generator.dao.BookEntityDao;
import io.wdb.modules.generator.generator.dao.BookImageAttachDao;
import io.wdb.modules.generator.generator.dao.BookInfoDao;
import io.wdb.modules.generator.generator.dao.BookReadableDao;
import io.wdb.modules.generator.generator.dao.BookTagDao;
import io.wdb.modules.generator.generator.dao.BookTopicalDao;
import io.wdb.modules.generator.generator.entity.BookImageAttachEntity;
import io.wdb.modules.generator.generator.entity.BookReadableEntity;
import io.wdb.modules.generator.generator.entity.customer.BookEntityCustomer;
import io.wdb.modules.generator.generator.entity.customer.BookInfoEntityCustomer;
import io.wdb.modules.generator.generator.entity.customer.BookTagEntityCustomer;
import io.wdb.modules.generator.generator.entity.customer.BookTopicEntityCustomer;
import io.wdb.modules.generator.generator.entity.web.WebResponse;
import io.wdb.modules.generator.generator.service.book.BookContentService;

@Service
public class BookContentServiceImpl implements BookContentService{

	@Autowired
	private BookTopicalDao bookTopicalDao;
	
	@Autowired
	private BookTagDao bookTagDao;
	
	@Autowired
	private BookEntityDao bookEntityDao;
	
	@Autowired
	private BookImageAttachDao bookImageAttachDao;
	
	@Autowired
	private BookReadableDao bookReadableDao;
	
	@Autowired
	private BookInfoDao bookInfoDao;
	
	@Override
	public List<BookInfoEntityCustomer> list(Query query) {
		PageHelper.startPage(PageHelperUtil.getPageNum(query), PageHelperUtil.getPageSize(query));
		List<BookInfoEntityCustomer> bookInfoEntityCustomers = bookInfoDao.queryBookContentList(query);
		//遍历查询主题和标签
		/*for(BookEntityCustomer bookEntityCustomer:bookEntityCustomers) {
			List<BookTopicEntityCustomer> bookTopicEntityCustomers = bookTopicalDao.getBookTopicByIsbn(bookEntityCustomer.getIsbnNo());
			StringBuffer bufferSubject = new StringBuffer();
			if(bookTopicEntityCustomers!=null&&bookTopicEntityCustomers.size()>0) {
				for(BookTopicEntityCustomer bookTopicEntityCustomer:bookTopicEntityCustomers) {
					if(bufferSubject.length()>0) {
						bufferSubject.append(",");
					}
					bufferSubject.append(bookTopicEntityCustomer.getBookTopicName());
				}
				bookEntityCustomer.setBookSubject(bufferSubject.toString());
			}
			List<BookTagEntityCustomer> bookTagEntityCustomers = bookTagDao.getBookTagByIsbn(bookEntityCustomer.getIsbnNo());
			if(bookTagEntityCustomers!=null&&bookTagEntityCustomers.size()>0) {
				StringBuffer bufferTag = new StringBuffer();
				for(BookTagEntityCustomer bookTagEntityCustomer:bookTagEntityCustomers) {
					if(bufferTag.length()>0) {
						bufferTag.append(",");
					}
					bufferTag.append(bookTagEntityCustomer.getBookTagName());
				}
				bookEntityCustomer.setBookTag(bufferTag.toString());
			}
		}*/
		return bookInfoEntityCustomers;
	}

	@Override
	public BookEntityCustomer getInfoByIsbn(String isbn) {
		BookEntityCustomer bookEntityCustomer = bookEntityDao.getInfoByIsbn(isbn);
		List<BookImageAttachEntity> attachEntities = bookImageAttachDao.getImagesByIsbn(isbn);
		StringBuffer buffer = new StringBuffer();
		for(BookImageAttachEntity attachEntity:attachEntities) {
			if(buffer.length()>0) {
				buffer.append(",");
			}
			buffer.append(attachEntity.getImageUrl());
		}
		bookEntityCustomer.setInnerImgs(buffer.toString());
		return bookEntityCustomer;
	}

	@Override
	public WebResponse updateInfo(BookEntityCustomer bookEntityCustomer) {
		//修改年龄段
		BookReadableEntity bookReadableEntityDb = bookReadableDao.getBookReadbaleByIsbn(bookEntityCustomer.getIsbnNo());
		bookReadableEntityDb.setModifyTime(new Date());
		bookReadableEntityDb.setMaxAge(bookEntityCustomer.getMaxAge());
		bookReadableEntityDb.setMinAge(bookEntityCustomer.getMinAge());
		bookReadableDao.updateBookReadable(bookReadableEntityDb);
		//修改作者简介
		
		return null;
	}

}
